package com.example.exercise.demo.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;

import com.example.exercise.demo.vo.AppThumbupVO;
import com.example.exercise.demo.dao.entity.AppThumbupEntity;
import com.example.exercise.demo.dao.AppThumbupDAO;

/**
* 
*/
@Service
public class AppThumbupService {

    @Autowired
    private AppThumbupDAO appThumbupDAO;

    public List<AppThumbupVO> findByWhere(Page<?> page, AppThumbupVO appThumbupVO){
        List<AppThumbupVO> list = new ArrayList<>();
        List<AppThumbupEntity> list0 = appThumbupDAO.findByWhere(page, appThumbupVO);
        for (AppThumbupEntity appThumbupEntity : list0) {
            list.add(toAppThumbupVO(appThumbupEntity));
        }
        return list;
    }

    public AppThumbupVO findByKey(AppThumbupVO appThumbupVO){
        AppThumbupEntity appThumbupEntity = appThumbupDAO.findByKey(appThumbupVO);
        return toAppThumbupVO(appThumbupEntity);
    }

    public int insert(AppThumbupVO appThumbupVO){
        return appThumbupDAO.insert(toAppThumbupEntity(appThumbupVO));
    }

    public int updateByKey(AppThumbupVO appThumbupVO){
        return appThumbupDAO.updateByKey(toAppThumbupEntity(appThumbupVO));
    }

    public int deleteByKey(AppThumbupVO appThumbupVO){
        return appThumbupDAO.deleteByKey(appThumbupVO);
    }

    private AppThumbupVO toAppThumbupVO(AppThumbupEntity appThumbupEntity) {
        AppThumbupVO appThumbupVO = new AppThumbupVO();
                appThumbupVO.setId(String.valueOf(appThumbupEntity.getId()));
        appThumbupVO.setUserId(String.valueOf(appThumbupEntity.getUserId()));
        appThumbupVO.setUserName(appThumbupEntity.getUserName());
        appThumbupVO.setThumbupType(String.valueOf(appThumbupEntity.getThumbupType()));
        appThumbupVO.setRelationId(String.valueOf(appThumbupEntity.getRelationId()));

        return appThumbupVO;
    }

    private AppThumbupEntity toAppThumbupEntity(AppThumbupVO appThumbupVO) {
        AppThumbupEntity appThumbupEntity = new AppThumbupEntity();
                String id = appThumbupVO.getId();
        if(id != null) {
            appThumbupEntity.setId(Integer.parseInt(appThumbupVO.getId()));
        }
        String userId = appThumbupVO.getUserId();
        if(userId != null) {
            appThumbupEntity.setUserId(Integer.parseInt(appThumbupVO.getUserId()));
        }
        appThumbupEntity.setUserName(appThumbupVO.getUserName());
        String thumbupType = appThumbupVO.getThumbupType();
        if(thumbupType != null) {
            appThumbupEntity.setThumbupType(Integer.parseInt(appThumbupVO.getThumbupType()));
        }
        String relationId = appThumbupVO.getRelationId();
        if(relationId != null) {
            appThumbupEntity.setRelationId(Integer.parseInt(appThumbupVO.getRelationId()));
        }

        return appThumbupEntity;
    }

}